package com.example.zjsr.util;

import com.example.zjsr.domain.WordFrequencyPic;
import com.huaban.analysis.jieba.JiebaSegmenter;
import com.kennycason.kumo.WordFrequency;
import com.kennycason.kumo.nlp.FrequencyAnalyzer;
import com.kennycason.kumo.nlp.tokenizers.ChineseWordTokenizer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WordFrequencyAnalyzer {


    public static WordFrequencyPic getWordFrequencyAnalyzer(String word) throws IOException {
        WordFrequencyPic wordFrequencyPic = new WordFrequencyPic();
        FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
        int count = 0;
//        if (word.length() > 10000) {
//            word = word.substring(0, 10000);
//        } else {
//            word = word.substring(0, word.length());
//        }
        String regEx = "[\n`~@!$%^&*()+-=|丨{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。， 、？]";
        String aa = "";//这里是将特殊字符换为aa字符串,""代表直接去掉
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(word);//这里把想要替换的字符串传进来
        String newString = m.replaceAll(aa).trim();
        JiebaSegmenter segmenter = new JiebaSegmenter();
        List<String> str = segmenter.sentenceProcess(newString);
        frequencyAnalyzer.setWordFrequenciesToReturn(600);
        frequencyAnalyzer.setMinWordLength(2);
        frequencyAnalyzer.setMaxWordLength(6);
        frequencyAnalyzer.setWordTokenizer(new ChineseWordTokenizer());
        final List<WordFrequency> wordFrequenciesList = frequencyAnalyzer.load(str);
        List<WordFrequency> wordFrequencies = new ArrayList<>();
        int j = 0;
        for (int i = 0; i < wordFrequenciesList.size(); i++) {
            if (j < 50) {
                wordFrequencies.add(wordFrequenciesList.get(i));
                count += wordFrequencies.get(j).getFrequency();
                j++;
            }
        }
        wordFrequencyPic.setWordFrequencyList(wordFrequencies);
        wordFrequencyPic.setCount(count);
        return wordFrequencyPic;
    }
}
